package com.gengzp.ops.controller;

import com.gengzp.common.constants.PermissionConstants;
import com.gengzp.common.document.annotations.Api;
import com.gengzp.common.document.annotations.ApiOperation;
import com.gengzp.common.model.base.req.SearchByIdReq;
import com.gengzp.common.model.base.resp.Result;
import com.gengzp.common.model.database.req.AddSysTableReq;
import com.gengzp.common.model.database.req.EditSysTableReq;
import com.gengzp.common.model.database.req.PageSysTableReq;
import com.gengzp.common.model.database.resp.SearchSysTableResp;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.ops.service.ISysTableService;
import com.gengzp.permission.annotations.CheckPermission;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName SysTableController
 * @Description 数据库表设计模块控制层
 * @Author gengzp
 * @Date 2025/7/8 13:34
 */
@RestController
@RequestMapping("/table")
@Api(tags = "数据库表设计模块")
public class SysTableController {

    @Autowired
    private ISysTableService sysTableService;

    /**
     * 添加表设计信息
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加数据库表设计信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> addSysTable(@Valid @RequestBody AddSysTableReq req) {
        sysTableService.addSysTable(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑表设计信息
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑数据库表设计信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> editSysTable(@Valid @RequestBody EditSysTableReq req) {
        sysTableService.editSysTable(req);
        return Result.getSuccessResult();
    }

    /**
     * 删除表设计信息
     */
    @PostMapping("/delete_by_id")
    @ApiOperation(value = "删除数据库表设计信息")
    @CheckPermission(PermissionConstants.DATABASE_WRITE)
    public Result<Void> deleteSysTableById(@Valid @RequestBody SearchByIdReq req) {
        sysTableService.deleteSysTableById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 根据id查询表设计详情
     */
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询数据库表设计详情")
    @CheckPermission(PermissionConstants.DATABASE_READ)
    public Result<SearchSysTableResp> searchSysTableById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(sysTableService.searchSysTableById(Long.valueOf(req.getId())));
    }

    /**
     * 分页查询表设计信息
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询数据库表设计信息")
    @CheckPermission(PermissionConstants.DATABASE_READ)
    public Result<PaginationResult<SearchSysTableResp>> pageQuerySysTable(@Valid @RequestBody PageSysTableReq req) {
        return Result.getSuccessResult(sysTableService.pageQuerySysTable(req));
    }

}
